<html>
<head><meta charset="utf-8"><title>How to write goto · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html">How to write goto</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223184067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223184067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223184067">(Jan 19 2021 at 03:28)</a>:</h4>
<p>What's the most reliable way to write code that compiles like break-with-value but without the nesting restrictions? I'm pretty sure it's possible to use break-with-value to encode arbitrary goto but the nesting makes it difficult to get to any of a set of labels. I've tried using <code>loop { state = match state { ... } }</code> which is pretty good in a pinch but doesn't always get optimized away to the same thing that you would get with break-with-value.</p>



<a name="223184596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223184596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223184596">(Jan 19 2021 at 03:43)</a>:</h4>
<p>Hum, are you looking for labeled break? Like <a href="https://doc.rust-lang.org/rust-by-example/flow_control/loop/nested.html">https://doc.rust-lang.org/rust-by-example/flow_control/loop/nested.html</a></p>



<a name="223184733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223184733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223184733">(Jan 19 2021 at 03:46)</a>:</h4>
<p>that's what I mean by break-with-value. I'm aware of it but it doesn't capture all control flow, for example if I wanted to implement an async state machine manually</p>



<a name="223184744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223184744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223184744">(Jan 19 2021 at 03:46)</a>:</h4>
<p>or maybe it does and I'm not creative enough</p>



<a name="223184761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223184761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223184761">(Jan 19 2021 at 03:47)</a>:</h4>
<p>The main issue is that you can't use <code>break 'label</code> outside the <code>'label: loop</code> block</p>



<a name="223184819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223184819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223184819">(Jan 19 2021 at 03:48)</a>:</h4>
<p>since actually breaking to a label takes you out of that block, you can't break to it again</p>



<a name="223184831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223184831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223184831">(Jan 19 2021 at 03:48)</a>:</h4>
<p>you can <code>continue 'label</code> but that doesn't carry a value</p>



<a name="223185100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185100">(Jan 19 2021 at 03:54)</a>:</h4>
<p>oh I see. If it's just about carrying a value, you can probably shove that value in a mutable variable? A bit sad but I'm not sure what you're trying to do</p>



<a name="223185127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185127">(Jan 19 2021 at 03:55)</a>:</h4>
<p>I think I'm too much of a functional programmer to properly conceive of arbitrary goto x)</p>



<a name="223185129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185129">(Jan 19 2021 at 03:55)</a>:</h4>
<p>The usual translation would be to wrap a whole function body in the loop, have the top-level statement be a match on "state", and translate goto to set state then break.</p>



<a name="223185168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185168">(Jan 19 2021 at 03:56)</a>:</h4>
<p>That still requires dividing the code to fit into the match arms.</p>



<a name="223185189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185189">(Jan 19 2021 at 03:56)</a>:</h4>
<p>So you can't put targets everywhere. For that you need further transformation.</p>



<a name="223185264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185264">(Jan 19 2021 at 03:58)</a>:</h4>
<p>do you have an example of code you'd like to write?</p>



<a name="223185266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185266">(Jan 19 2021 at 03:58)</a>:</h4>
<p>As a functional programmer myself, I think goto is very naturally expressed as a <code>let rec</code> of tail recursive functions</p>



<a name="223185359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185359">(Jan 19 2021 at 04:00)</a>:</h4>
<p>Right <span class="user-mention" data-user-id="239881">@Josh Triplett</span> , that's a good way to write it, and I use that in practice. The sadness is that this doesn't compile as well as it should, at least in some small tests with LLVM</p>



<a name="223185385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185385">(Jan 19 2021 at 04:01)</a>:</h4>
<p>This kind of thing comes up when you are writing an interpreter, where the cost of an extra basic block for the outer loop is measurable</p>



<a name="223185437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185437">(Jan 19 2021 at 04:02)</a>:</h4>
<p>The problem that got me thinking about it today was writing an iterative (manual stack) version of a recursive algorithm</p>



<a name="223185963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223185963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223185963">(Jan 19 2021 at 04:16)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b34fd9438b17a362241859a7e5e78730">Here's an example</a> of such a transformation on a dfs algorithm</p>



<a name="223186124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223186124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223186124">(Jan 19 2021 at 04:21)</a>:</h4>
<p>As for "code I'd like to write", the main goal is to be able to write, by some means, (something that optimizes to) any control flow that can be expressed in MIR</p>



<a name="223186670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223186670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223186670">(Jan 19 2021 at 04:35)</a>:</h4>
<p>If I had to invent syntax for goto, I have no idea what fits best, but something like the following would do the trick:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">dfs_iterative</span><span class="p">(</span><span class="n">graph</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="o">&amp;</span><span class="p">[</span><span class="kt">usize</span><span class="p">]],</span><span class="w"> </span><span class="n">mark</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="kt">bool</span><span class="p">],</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">i</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">stack</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[];</span><span class="w"></span>

<span class="w">  </span><span class="o">'</span><span class="na">start</span>: <span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">goto</span><span class="w"> </span><span class="o">'</span><span class="na">iter</span><span class="w"> </span><span class="n">graph</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">iter</span><span class="p">()</span><span class="w"></span>
<span class="w">  </span><span class="p">};</span><span class="w"></span>
<span class="w">  </span><span class="o">'</span><span class="na">iter</span>: <span class="o">|</span><span class="k">mut</span><span class="w"> </span><span class="n">it</span>: <span class="nc">std</span>::<span class="n">slice</span>::<span class="n">Iter</span><span class="o">&lt;'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span><span class="o">&gt;|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="o">&amp;</span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">it</span><span class="p">.</span><span class="n">next</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="n">mark</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">stack</span><span class="p">.</span><span class="n">push</span><span class="p">((</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">it</span><span class="p">));</span><span class="w"></span>
<span class="w">        </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">goto</span><span class="w"> </span><span class="o">'</span><span class="na">start</span><span class="w"></span>
<span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">goto</span><span class="w"> </span><span class="o">'</span><span class="na">iter</span><span class="w"> </span><span class="n">it</span><span class="w"></span>
<span class="w">      </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="n">goto</span><span class="w"> </span><span class="o">'</span><span class="na">done</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">  </span><span class="p">};</span><span class="w"></span>
<span class="w">  </span><span class="o">'</span><span class="na">done</span>: <span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">mark</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">stack</span><span class="p">.</span><span class="n">pop</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="nb">None</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">return</span><span class="p">,</span><span class="w"></span>
<span class="w">      </span><span class="nb">Some</span><span class="p">((</span><span class="n">j</span><span class="p">,</span><span class="w"> </span><span class="n">it</span><span class="p">))</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">goto</span><span class="w"> </span><span class="o">'</span><span class="na">iter</span><span class="w"> </span><span class="n">it</span><span class="w"></span>
<span class="w">      </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">  </span><span class="p">};</span><span class="w"></span>

<span class="w">  </span><span class="n">goto</span><span class="w"> </span><span class="o">'</span><span class="na">start</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="223186788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223186788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223186788">(Jan 19 2021 at 04:38)</a>:</h4>
<p>The main thrust of my question was rather what mechanisms that already exist in rust can be abused to get goto functionality, rather than imagining new syntax that I don't expect to ever make it past the folks who like to quote dijkstra</p>



<a name="223187895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223187895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223187895">(Jan 19 2021 at 05:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/How.20to.20write.20goto/near/223185385">said</a>:</p>
<blockquote>
<p>This kind of thing comes up when you are writing an interpreter, where the cost of an extra basic block for the outer loop is measurable</p>
</blockquote>
<p>Ah, you're talking about things like the computed goto used in the Python bytecode interpreter, to jump directly to the right branch for the next instruction?</p>



<a name="223188266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223188266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223188266">(Jan 19 2021 at 05:14)</a>:</h4>
<p>yeah, i wrote a regex compiler proc macro at once point and the terrible perf of the <code>loop { state = match state {} }</code> approach (and several other options i tried) compared to goto-using C code was enough that i never bothered finishing it.</p>



<a name="223188322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223188322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223188322">(Jan 19 2021 at 05:16)</a>:</h4>
<p>computed goto would also be nice for interpreters and the like. i've also run into the desire for such things in a ANS-style decoder at one point</p>



<a name="223188340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223188340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223188340">(Jan 19 2021 at 05:16)</a>:</h4>
<p>What's more, although it doesn't come up super often, because the borrow checker works on MIR it is often at its most powerful intraprocedurally when you are using complex control flow. Using <code>state = match state</code> erases the information of which blocks go where so some code will be rejected</p>



<a name="223188360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223188360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223188360">(Jan 19 2021 at 05:18)</a>:</h4>
<p>In the example above we don't need any computed goto, although that would be pretty nice, it's really just a goto to a known location, it just isn't a location that lends itself well to hierarchy.</p>



<a name="223188736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223188736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223188736">(Jan 19 2021 at 05:27)</a>:</h4>
<p>I think you could even make computed goto a typesafe thing, if there is a type/calling convention for "label closures" inside a function. They are basically FnOnce closures that capture the entire context and return <code>!</code></p>



<a name="223188784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223188784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223188784">(Jan 19 2021 at 05:28)</a>:</h4>
<p>the complication being that they don't have unique types, they are like <code>fn()</code> in that their concrete representation is a code pointer</p>



<a name="223189472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223189472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223189472">(Jan 19 2021 at 05:47)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> This seems like a pattern that MIR optimization could be taught to recognize.</p>



<a name="223189519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223189519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223189519">(Jan 19 2021 at 05:48)</a>:</h4>
<p><code>loop { state = match state { ... } }</code> or things that generate equivalent MIR could be transformed to have direct state-to-state gotos.</p>



<a name="223189525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223189525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223189525">(Jan 19 2021 at 05:48)</a>:</h4>
<p>that would be fantastic</p>



<a name="223189564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223189564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223189564">(Jan 19 2021 at 05:49)</a>:</h4>
<p>You might inquire in <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt">https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt</a> , and provide some sample code for the pattern.</p>



<a name="223189611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223189611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223189611">(Jan 19 2021 at 05:50)</a>:</h4>
<p>I believe there's interest in that stream in identifying appropriate new optimizations.</p>



<a name="223327811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20write%20goto/near/223327811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> erikdesjardins <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20write.20goto.html#223327811">(Jan 20 2021 at 03:24)</a>:</h4>
<p>see also <a href="https://github.com/rust-lang/rust/pull/80475">https://github.com/rust-lang/rust/pull/80475</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>